package activity.CustomView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import utils.HiTools;

/**
 * Created by lt on 2018\3\19 0019.
 */

public class CropView extends View {
    private Context mContext;
    private Paint mPaint;
    private Paint mCirclePaint;
    private int mCurrentAngle = 0;//当前是那个角
    //四个角
    private final int ANGLE_LEFT_TOP = 1;
    private final int ANGLE_LEFT_BOTT = 2;
    private final int ANGLE_RIGHT_TOP = 3;
    private final int ANGLE_RIGHT_BOTT = 4;
    //private final int RECT_INNER=5;
    private boolean isGoke = true;
    private int u32Resolution;
    private int mainSteamWidth;
    private int mainSteamHeight;

    public CropView(Context context) {
        super(context);
        init(context);
    }

    public CropView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#5CACEE"));
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(HiTools.dip2px(mContext, 3));
        mPaint.setStyle(Paint.Style.STROKE);

        mCirclePaint = new Paint();
        mCirclePaint.setColor(Color.parseColor("#5CACEE"));
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStrokeWidth(CIRCLE_R);
        mCirclePaint.setStyle(Paint.Style.FILL);
        RECT_LEFT = HiTools.dip2px(mContext, 10);
        RECT_TOP = HiTools.dip2px(mContext, 10);
        RECT_BOTT = HiTools.dip2px(mContext, 190);
        RECT_RIGHT = HiTools.dip2px(mContext, 290);
        CIRCLE_R = HiTools.dip2px(mContext, 7);
    }

    private float RECT_LEFT;
    private float RECT_TOP;
    private float RECT_BOTT;
    private float RECT_RIGHT;
    private float CIRCLE_R;

    private float top;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isGoke) {
            float dis=( HiTools.dip2px(mContext, 180)*50/(float)mainSteamHeight);
            Log.i("tedu","--isGoke--"+"--dis--"+dis);
            top = HiTools.dip2px(mContext, 10) +dis;
            RECT_TOP = top;
        } else {
            top = HiTools.dip2px(mContext, 10) + 30 / mainSteamHeight * (HiTools.dip2px(mContext, 190));
            RECT_TOP = top;
        }
        Log.i("tedu", "-onDraw-:" +"-mainSteamHeight-:"+mainSteamHeight+"-RECT_TOP-:"+RECT_TOP);
        switch (mCurrentAngle) {
            case ANGLE_LEFT_TOP://左上
                RECT_LEFT += dis_X;
                RECT_TOP += dis_Y;
                if (RECT_BOTT - RECT_TOP <= 100) {//确定最小面积
                    RECT_TOP -= dis_Y;
                }
                if (RECT_RIGHT - RECT_LEFT <= 100) {//确定最小面积
                    RECT_LEFT -= dis_X;
                }
                break;
            case ANGLE_LEFT_BOTT://左下
                RECT_LEFT += dis_X;
                RECT_BOTT += dis_Y;
                if (RECT_BOTT - RECT_TOP <= 100) {//确定最小面积
                    RECT_BOTT -= dis_Y;
                }
                if (RECT_RIGHT - RECT_LEFT <= 100) {//确定最小面积
                    RECT_LEFT -= dis_X;
                }
                break;
            case ANGLE_RIGHT_TOP://右上
                RECT_TOP += dis_Y;
                RECT_RIGHT += dis_X;
                if (RECT_BOTT - RECT_TOP <= 100) {//确定最小面积
                    RECT_TOP -= dis_Y;
                }
                if (RECT_RIGHT - RECT_LEFT <= 100) {//确定最小面积
                    RECT_RIGHT -= dis_X;
                }
                break;
            case ANGLE_RIGHT_BOTT://右下
                RECT_RIGHT += dis_X;
                RECT_BOTT += dis_Y;
                if (RECT_BOTT - RECT_TOP <= 100) {//确定最小面积
                    RECT_BOTT -= dis_Y;
                }
                if (RECT_RIGHT - RECT_LEFT <= 100) {//确定最小面积
                    RECT_RIGHT -= dis_X;
                }
                break;
        }
        if (RECT_LEFT <= HiTools.dip2px(mContext, 10)) {
            RECT_LEFT = HiTools.dip2px(mContext, 10);
            dis_X = 0;
            dis_Y = 0;
        }
//        if (RECT_TOP <= top) {
//            RECT_TOP = top;
//            dis_X = 0;
//            dis_Y = 0;
//        }
        if (RECT_RIGHT >= HiTools.dip2px(mContext, 290)) {
            RECT_RIGHT = HiTools.dip2px(mContext, 290);
            dis_X = 0;
            dis_Y = 0;
        }
        if (RECT_BOTT >= HiTools.dip2px(mContext, 190)) {
            RECT_BOTT = HiTools.dip2px(mContext, 190);
            dis_X = 0;
            dis_Y = 0;
        }
        Log.i("tedu", "-onDraw-:" + "-RECT_LEFT-:" + RECT_LEFT + "-RECT_TOP-:" + RECT_TOP + "-RECT_RIGHT-:" + RECT_RIGHT + "-RECT_BOTT-:" + RECT_BOTT + "-dis_X-:" + dis_X + "-dis_Y-:" + dis_Y);
        canvas.drawRect(RECT_LEFT, RECT_TOP, RECT_RIGHT, RECT_BOTT, mPaint);
        canvas.drawCircle(RECT_LEFT, RECT_TOP, CIRCLE_R, mCirclePaint);//左上
        canvas.drawCircle(RECT_RIGHT, RECT_TOP, CIRCLE_R, mCirclePaint);//右上
        canvas.drawCircle(RECT_LEFT, RECT_BOTT, CIRCLE_R, mCirclePaint);//左下
        canvas.drawCircle(RECT_RIGHT, RECT_BOTT, CIRCLE_R, mCirclePaint);//右下
    }

    private float down_X;
    private float down_Y;
    private float move_X;
    private float move_Y;
    private float dis_X;
    private float dis_Y;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 1) {//一个手指
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    down_X = event.getX();
                    down_Y = event.getY();
                    mCurrentAngle = getTouchPoint(event.getX(), event.getY());
                    Log.i("tedu", "--MotionEvent.ACTION_DOWN--:" + mCurrentAngle);
                    break;
                case MotionEvent.ACTION_MOVE:
                    move_X = event.getX();
                    move_Y = event.getY();
                    dis_X = (move_X - down_X);
                    dis_Y = (move_Y - down_Y);
                    if (dis_X != 0 || dis_Y != 0) {
                        switch (mCurrentAngle) {
                            case ANGLE_LEFT_TOP://左上
                            case ANGLE_RIGHT_TOP://右上
                            case ANGLE_LEFT_BOTT://左下
                            case ANGLE_RIGHT_BOTT://右下
                                invalidate();
                                break;
                        }
                    }
                    down_X = move_X;
                    down_Y = move_Y;
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }

        }
        return true;
    }

    //根据按下时触摸点,判断按下时RECT的那个角
    public int getTouchPoint(float eventX, float eventY) {
        if (eventX >= RECT_LEFT - CIRCLE_R * 2 && eventX <= RECT_LEFT + CIRCLE_R * 2 && eventY >= RECT_TOP - CIRCLE_R * 2 && eventY <= RECT_TOP + CIRCLE_R * 2) {
            return ANGLE_LEFT_TOP;
        } else if (eventX >= RECT_LEFT - CIRCLE_R * 2 && eventX <= RECT_LEFT + CIRCLE_R * 2 && eventY >= RECT_BOTT - CIRCLE_R * 2 && eventY <= RECT_BOTT + CIRCLE_R * 2) {
            return ANGLE_LEFT_BOTT;
        } else if (eventX > RECT_RIGHT - CIRCLE_R * 2 && eventX <= RECT_RIGHT + CIRCLE_R * 2 && eventY >= RECT_TOP - CIRCLE_R * 2 && eventY <= RECT_TOP + CIRCLE_R * 2) {
            return ANGLE_RIGHT_TOP;
        } else if (eventX > RECT_RIGHT - CIRCLE_R * 2 && eventX <= RECT_RIGHT + CIRCLE_R * 2 && eventY > RECT_BOTT - CIRCLE_R * 2 && eventY < RECT_BOTT + CIRCLE_R * 2) {
            return ANGLE_RIGHT_BOTT;
        } else if (eventX > RECT_LEFT + CIRCLE_R * 2 && eventX < RECT_RIGHT - CIRCLE_R * 2 && eventY > RECT_TOP + CIRCLE_R * 2 && eventY < RECT_BOTT - CIRCLE_R * 2) {
        }
        return 0;
    }

    public void setArea(int area) {
        switch (area) {
            case 1://左区域
                RECT_LEFT = HiTools.dip2px(mContext, 10);
                RECT_TOP = HiTools.dip2px(mContext, 10);
                RECT_BOTT = HiTools.dip2px(mContext, 190);
                RECT_RIGHT = HiTools.dip2px(mContext, 290) / 2;
                break;
            case 2://全部区域
                RECT_LEFT = HiTools.dip2px(mContext, 10);
                RECT_TOP = HiTools.dip2px(mContext, 10);
                RECT_BOTT = HiTools.dip2px(mContext, 190);
                RECT_RIGHT = HiTools.dip2px(mContext, 290);
                break;
            case 3://右区域
                RECT_LEFT = HiTools.dip2px(mContext, 10) + HiTools.dip2px(mContext, 290) / 2;
                RECT_TOP = HiTools.dip2px(mContext, 10);
                RECT_BOTT = HiTools.dip2px(mContext, 190);
                RECT_RIGHT = HiTools.dip2px(mContext, 290);
                break;
        }
        invalidate();
    }

    //限制OSD的高度
    public void isPaddTop(boolean isGoke, int u32Resolution) {
        this.isGoke = isGoke;
        this.u32Resolution = u32Resolution;
        switch (u32Resolution) {
            case 0:
                mainSteamWidth = 640;
                mainSteamHeight = 480;
                break;
            case 1:
                mainSteamWidth = 320;
                mainSteamHeight = 240;
                break;
            case 2:
                mainSteamWidth = 160;
                mainSteamHeight = 112;
                break;
            case 4:
                mainSteamWidth = 352;
                mainSteamHeight = 288;
                break;
            case 3:
                mainSteamWidth = 704;
                mainSteamHeight = 576;
                break;
            case 6:
                mainSteamWidth = 1280;
                mainSteamHeight = 720;
                break;
            case 7:
                mainSteamWidth = 640;
                mainSteamHeight = 352;
                break;
            case 8:
                mainSteamWidth = 320;
                mainSteamHeight = 176;
                break;
            case 14:
                mainSteamWidth = 1280;
                mainSteamHeight = 960;
                break;
            case 13:
                mainSteamWidth = 1920;
                mainSteamHeight = 1080;
                break;
        }
        invalidate();
    }
}
